package com.atguigu.controller;

import com.atguigu.client.ProductFeignClient;
import com.atguigu.client.SearchFeignClient;
import com.atguigu.result.RetVal;
import com.atguigu.search.SearchParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class IndexController {
    @Autowired
    private ProductFeignClient productFeignClient;
    @Autowired
    SearchFeignClient searchFeignClient;
    @GetMapping({"/", "index.html"})
    public String index(HttpServletRequest request){
        RetVal retVal = productFeignClient.getIndexCategoryInfo();
        request.setAttribute("list", retVal.getData());
        return "index/index";
    }

    //搜索
    @GetMapping("search.html")
    public String searchProduct(SearchParam searchParam, Model model){
        RetVal<Map> retVal=searchFeignClient.searchParam(searchParam);
        //页面设置搜索到的商品信息
        model.addAllAttributes(retVal.getData());
        model.addAttribute("searchParam",searchParam);
        //搜索路径参数
        String urlParam=pageUrlParam(searchParam);
        model.addAttribute("urlParam",urlParam);
        //设置品牌显示
        String brandName = pageBrandName(searchParam.getBrandName());
        model.addAttribute("brandNameParam",brandName);

        List<Map<String, String>> propsList = pageProps(searchParam.getProps());
        model.addAttribute("propsParamList",propsList);

        Map<String, Object> stringObjectMap = pageSortInfo(searchParam.getOrder());
        model.addAttribute("orderMap",stringObjectMap);
        return "search/index";
    }

    //3.获取平台属性数据
    private List<Map<String, String>> pageProps(String[] props) {
        List<Map<String,String>> list = new ArrayList<>();
        if (props!=null && props.length>0){
            for (String prop : props) {
                String[] split = prop.split(":");
                if (split!=null && split.length==3){
                    HashMap<String, String> map = new HashMap<>();
                    map.put("propertyKeyId",split[0]);
                    map.put("propertyValue",split[1]);
                    map.put("propertyKey",split[2]);
                    list.add(map);
                }
            }
        }
        return list;
    }

    private String pageBrandName(String brandName) {
        if (!StringUtils.isEmpty(brandName)){
            String[] split = brandName.split(":");
            if (split!=null&&split.length==2){
                return "品牌:"+split[1];
            }
        }
        return "";

    }

    private String pageUrlParam(SearchParam searchParam) {
        StringBuilder urlParam=new StringBuilder();
        if (!StringUtils.isEmpty(searchParam.getKeyword())){
            urlParam.append("Keyword=").append(searchParam.getKeyword());
        }
        if (!StringUtils.isEmpty(searchParam.getCategory1Id())){
            urlParam.append("category1Id=").append(searchParam.getCategory1Id());
        }

        if (!StringUtils.isEmpty(searchParam.getCategory2Id())){
            urlParam.append("category2Id=").append(searchParam.getCategory2Id());
        }

        if (!StringUtils.isEmpty(searchParam.getCategory3Id())){
            urlParam.append("category3Id=").append(searchParam.getCategory3Id());
        }
        //判断拼接品牌信息
        if (!StringUtils.isEmpty(searchParam.getBrandName())){
            if (urlParam.length()>0){
                urlParam.append("&brandName=").append(searchParam.getBrandName());
            }
        }
        if (!StringUtils.isEmpty(searchParam.getProps())){
                for (String prop : searchParam.getProps()) {
                    if (urlParam.length()>0){
                    urlParam.append("&props=").append(prop);
                }
            }
        }
        return "search.html?"+urlParam.toString();

    }

private Map<String,Object> pageSortInfo(String order){
        Map<String,Object>orderMap=new HashMap<>();
        if (!StringUtils.isEmpty(orderMap)){
            String[] orderParams = order.split(":");
            if (orderParams.length==2){
                orderMap.put("type",orderParams[0]);
                orderMap.put("sort",orderParams[1]);
            }else {
                orderMap.put("type","1");
                orderMap.put("sort","asc");
            }
        }
        return orderMap;
}












}
